package com.das.mechanic_base.adapter.create;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.DetectionNewBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class FirstAloneSelectAdapter extends RecyclerView.Adapter<FirstAloneSelectAdapter.FirstAloneHolder> {

    private List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> mList;

    // 是否有添加功能
    private boolean isAdd;

    /**
     * 0 添加故障灯
     * 1 添加贵重物品
     * 2 随车工具
     */
    private int type;

    private Context mContext;
    private String url;

    public FirstAloneSelectAdapter(Context mContext,int type,boolean isAdd) {
        this.mContext = mContext;
        this.type = type;
        this.isAdd = isAdd;
        this.mList = new ArrayList<>();
    }

    public void changeData(List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> mList,String url){
        this.mList = mList;
        this.url = url;
        if (iOnClickFirstAlone != null) {
            iOnClickFirstAlone.iOnClickSelectThing(new ArrayList<>());
        }
        notifyDataSetChanged();
    }

    public List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> getmList() {
        return mList;
    }

    /**
     * 获取选中项
     * @return
     */
    public List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> getSelectList() {
        List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> sList = new ArrayList<>();
        if (X3StringUtils.isListEmpty(mList)) {
            return sList;
        }
        for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean listBean : mList) {
            if (!listBean.selected) {
                continue;
            }
            sList.add(listBean);
        }
        return sList;
    }

    /**
     * 添加条目
     * @param selectBean
     */
    public void addFirstAloneData(DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean selectBean) {
        this.mList.add(selectBean);
        notifyItemInserted(mList.size() - 1);
    }

    @NonNull
    @Override
    public FirstAloneHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FirstAloneHolder(LayoutInflater.from(mContext).inflate( R.layout.x3_first_alone_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull FirstAloneHolder holder, int position) {
        if (isAdd && position == getItemCount() - 1) {
            holder.iv_icon.setImageResource(R.mipmap.x3_iv_tab_task);
            holder.tv_title.setText(type == 0 ? mContext.getString(R.string.x3_add_error_light) : mContext.getString(R.string.x3_add_rich_thing));
            holder.iv_select.setVisibility(View.INVISIBLE);
            holder.itemView.setOnClickListener(v -> {
                if (iOnClickFirstAlone != null) {
                    iOnClickFirstAlone.iOnClickAddThing(type,url);
                }
            });

        }else {
            holder.iv_select.setVisibility(View.VISIBLE);

            String iconUrl = mList.get(position).iconUrl;
            if (X3StringUtils.isEmpty(iconUrl)) {
                holder.iv_icon.setImageResource((int) mList.get(position).iconRes);
            }else {
                X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(mList.get(position).iconUrl), holder.iv_icon, 0);
            }

            String answer = mList.get(position).answer;
            if (answer.contains("\\.")) {
                String[] split = answer.split("\\.");
                holder.tv_title.setText(split[1].trim() + "");
            }else if (answer.contains("_")) {
                String[] split = answer.split("_");
                holder.tv_title.setText(split[1].trim() + "");
            }else {
                holder.tv_title.setText(answer);
            }

            if (type == 0) {
                holder.iv_select.setImageResource(R.mipmap.x3_icon_delete);
                holder.iv_select.setOnClickListener(v -> {
                    mList.remove(position);
                    notifyItemRemoved(position);
                    notifyItemChanged(position,mList.size() - position);
                });

            }else {
                holder.iv_select.setImageResource(mList.get(position).selected ? R.mipmap.select_icon : R.mipmap.no_select_icon);
                holder.iv_select.setOnClickListener(v -> holder.itemView.performClick());

                holder.itemView.setOnClickListener(v -> {
                    DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean listBean = mList.get(position);
                    mList.remove(position);
                    listBean.selected = !listBean.selected;
                    mList.add(position,listBean);

                    notifyItemChanged(position);

                    List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> selectBeans = new ArrayList<>();
                    for (int i = 0; i < mList.size(); i++) {
                        if (!mList.get(i).selected) {
                            continue;
                        }
                        selectBeans.add(mList.get(i));
                    }

                    if (iOnClickFirstAlone != null) {
                        iOnClickFirstAlone.iOnClickSelectThing(selectBeans);
                    }
                });
            }

        }

    }

    IOnClickFirstAlone iOnClickFirstAlone;

    public FirstAloneSelectAdapter setiOnClickFirstAlone(IOnClickFirstAlone iOnClickFirstAlone) {
        this.iOnClickFirstAlone = iOnClickFirstAlone;
        return this;
    }

    public interface IOnClickFirstAlone {
        // 返回选中的资源
        void iOnClickSelectThing(List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> mList);

        // 添加
        void iOnClickAddThing(int type,String url);
    }

    @Override
    public int getItemCount() {
        return isAdd ? mList.size() + 1 : mList.size();
    }

    class FirstAloneHolder extends RecyclerView.ViewHolder {
        ImageView iv_icon;
        ImageView iv_select;
        TextView tv_title;
        public FirstAloneHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            iv_select = itemView.findViewById(R.id.iv_select);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }
}
